// WithdrawController.java
package com.imut.onlinebanking.dealings.controller;

import com.imut.onlinebanking.base.response.JsonResult;
import com.imut.onlinebanking.base.response.StatusCode;
import com.imut.onlinebanking.dealings.entity.Account;
import com.imut.onlinebanking.dealings.service.AccountsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;

@RestController
@RequestMapping("/withdraw")
public class WithdrawController {

    @Autowired
    private AccountsService accountService;

    @PostMapping
    public JsonResult withdraw(@RequestParam Long accountId, @RequestParam BigDecimal amount) {
        boolean result = accountService.withdraw(accountId, amount);
        if (result) {
            return JsonResult.ok("取款成功");
        } else {
            return new JsonResult(StatusCode.OPERATION_FAILED.getCode(), "取款失败，余额不足或账户不存在", null);
        }
    }
@GetMapping("/withdrawSuccess")
public String showWithdrawSuccess(Model model, @RequestParam Long accountId) {
    Account account = accountService.getAccountById(accountId);
    System.out.println("获取到的账户数据：" + account); // 打印查看是否有 operationTime 值
    model.addAttribute("account", account);
    return "withdrawSuccess";
}


    }
